<?php
/**
 * @author spenceryg
 * @date 2023/4/8
 * @time 5:35 下午
 * @focus love & life
 */


namespace app\api\controller;


use app\common\model\PushPlan;
use hg\apidoc\annotation as Apidoc;

/**
 * Class PushLog
 * @package app\api\controller
 * @Apidoc\Title("推送日志")
 * @Apidoc\Group("api")
 */
class PushLog extends Auth
{
    /**
     * list
     * @Apidoc\Title("日志列表")
     */
    public function list()
    {
        $start = input('start');
        $end = input('end');
        $article_class_id = input('article_class_id');
        $push_plan_id = input('push_plan_id');
        $site_id = input('site_id');
        $article_id = input('article_id');
        $state = input('state', -1);
        $list_rows = input('list_rows', 10, 'intval');
        $log = \app\common\model\PushLog::order('id desc');
        $log = $log->join('db_article', 'db_article.id = db_push_log.article_id');
        $log = $log->where('db_push_log.member_id', $this->request->uid);
        if($start || $end) {
            $time_between = [
                $start ? strtotime($start) : 0,
                $end ? strtotime($end." 23:59:59") : time() + 24 * 60 * 60 - 1
            ];
            $log = $log->where('db_push_log.create_time', 'between', $time_between);
        }
        if($article_class_id){
            $log = $log->where('db_article.article_class_id', $article_class_id );
        }
        if($push_plan_id) {
            $log = $log->where('db_push_log.push_plan_id',  $push_plan_id);
        }
        if($site_id) {
            $log = $log->where('db_push_log.site_id',  $site_id);
        }
        if($article_id) {
            $log = $log->where('db_article.id', $article_id );
        }
        if($state !== -1){
            $log = $log->where('db_push_log.state',  $state);
        }
        $log = $log->field([
            'db_push_log.*',
            'db_article.name as article_name',
            'db_article.id as article_id',
            'db_article.article_class_id as article_class_id'
        ]);

        $res = $log->paginate($list_rows);
        $res = $res->toArray();
        foreach ($res['data'] as $index => $item) {
            $item['site_name'] = \app\common\model\Site::where('id', $item['site_id'])->value('name');
            if($item['push_plan_id']) {
                $item['plan_name'] = PushPlan::where('id', $item['push_plan_id'])->value('name');
            }else {
                $item['plan_name'] = '手动发布';
            }
            $item['article_class_name'] = \app\common\model\ArticleClass::where('id', $item['article_class_id'])->value('name');
            $res['data'][$index] = $item;
        }
        return success('', $res);

    }
}
